package com.violet.aspect;

import com.violet.domain.dto.DTO;
import com.violet.utils.DTOVerifyUtils;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Stack;

@Aspect
@Component
@RequiredArgsConstructor
public class ControllerAspect {

    private final DTOVerifyUtils verifyUtils;

    // DTO 校验
    @Pointcut("execution(* com.violet.controller.*.*.*(.., com.violet.domain.dto.*.*, ..))")
    public void controllerMethods() {}

    @Before("controllerMethods()")
    public void before(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        // 拿到所有的 DTO 参数
        for (Object arg : args) {
            if (arg instanceof DTO) {
                // 校验参数
                verifyUtils.verify((DTO) arg);
            }
        }
    }

}
